$(function () {
    //加载专业
    findSubjectList(function (result) {
        var allSubject = result;
        var parentSubjectList = allSubject[0];
        if (parentSubjectList != null && parentSubjectList.length > 0) {
            var context = '<li data-id="" class="cm-filter-item parent-subject active">全部</li>';
            $.each(parentSubjectList , function (index,subject) {
                context += '<li data-id="'+subject.id+'" class="cm-filter-item parent-subject">'+subject.subjectName+'</li>';
            });
            $("#parent-subject-list-box").html(context);
            $("#parent-subject-list-box>.parent-subject").click(function (e) {
                $("#child-subject-list .cm-filter-item-list").html("");
                var _this = $(this);
                var parentId = _this.data('id');
                var childList = allSubject[parentId];
                _this.addClass("active").siblings().removeClass('active');
                $("#subjectId").val(parentId);
                querySmallClassData(1);
                if(childList!=null && childList.length > 0){
                    $("#child-subject-list").removeClass("hide");
                    context = '<li data-id='+parentId+' class="cm-filter-item active">全部</li>';
                    $.each(childList,function (index,child) {
                        context += '<li class="cm-filter-item" data-id="'+child.id+'">'+child.subjectName+'</li>';
                    });
                    $("#child-subject-list .cm-filter-item-list").html(context);
                    $("#child-subject-list .cm-filter-item-list>.cm-filter-item").click(function (e) {
                        _this = $(this);
                        _this.addClass("active").siblings().removeClass('active');
                        var subjectId = _this.data("id");
                        $("#subjectId").val(subjectId);
                        querySmallClassData(1);
                        e.stopPropagation();
                    });
                }else{
                    $("#child-subject-list").addClass("hide");
                }
                e.stopPropagation();
            });
        }
    });

    /**
     * 获取讲师数据列表
     */
    findAllTeacherList(function(teacherList){
        if(teacherList!=null && teacherList.length>0){
            var context = '<li class="cm-filter-item active" data-id="">全部</li>';
            $.each(teacherList,function (index,teacher) {
                context += '<li class="cm-filter-item" data-id="'+teacher.id+'">'+teacher.teacherName+'</li>';
            });
            $("#teacher-list").html(context);
            $("#teacher-list>li").click(function (e) {
                var _this = $(this);
                _this.addClass("active").siblings().removeClass('active');
                querySmallClassData(1);
                e.stopPropagation();
            });
        }
    },"1");

    $("body").on('click','.live-sort-item-bx',function () {//点击专业和讲师
        $(".live-sort-item-bx>span").removeClass("active");
        $(this).find('span').addClass('active');
    }).on('click','.order-by-box',function () {//排序
        var order = $(this).data("order");
        if(order==1){
            $(this).data("order",2);
        }else if(order==2){
            $(this).data("order",1);
        }else if(order==3){
            $(this).data("order",4);
        }else if(order==4){
            $(this).data("order",3);
        }
        $("#order").val(order);
        $(".order-by-box").removeClass("c-info").addClass("c-222");
        $(this).removeClass('c-222').addClass('c-info');
        querySmallClassData(1);
    });

    //加载数据
    querySmallClassData(1);

    //获取直播页面数据列表
    function querySmallClassData(pageNo) {
        var params = {};
        var subjectIds = $("#subjectId").val();
        params['subjectIds'] = subjectIds;
        var teacherId = $("#teacher-list .active").data('id');
        params['teacherIds'] = teacherId;
        var order = $("#order").val();
        params['orderType'] = order;
        params = JSON.stringify(params);
        getPageAjaxData({
            url: baseloaction + '/web/course/ajaxSmallClassIndex',
            contextId: 'small-class-list-context',
            pageNo: pageNo,
            pageCall: querySmallClassData,
            params: params,
            callback: function (_pageNo) {
                lazyCourseImage();
                lazyTeacherImage();

                $('[data-toggle=popover]').popover({html: true});
            }
        });
    }
});
